/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ListaRepositorio.java
 *
 * Created on 13/03/2010, 16:43:13
 */
package br.com.janalise.view;

import br.com.janalise.control.MapaDoTextoController;
import br.com.janalise.model.MapaDoTexto;
import br.com.janalise.model.Regra;
import br.com.janalise.system.Configurar;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import br.com.janalise.util.Arquivo;
import br.com.janalise.util.BuscarPadrao;
import br.com.janalise.util.Repositorio;
import java.awt.Image;
import java.awt.Toolkit;
import javax.swing.ImageIcon;
import javax.swing.JDialog;

/**
 *
 * @author boskyn9
 */
public class ListaRepositorio extends PaiFrame {

    /** Creates new form ListaRepositorio */
    public ListaRepositorio() {
        initComponents();
        new Repositorio().getTreeModelFiles(jTree1, repositorio,null);
    }

    public ListaRepositorio(List<MapaDoTexto> textos) {
        initComponents();
        new Repositorio().getTreeModelFiles(jTree1, repositorio,textos);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanelMain = new javax.swing.JPanel();
        jSplitPaneMain = new javax.swing.JSplitPane();
        jPanelTree = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTree1 = new javax.swing.JTree();
        jPanelTable = new javax.swing.JPanel();
        jPanelInformacao = new javax.swing.JPanel();
        jLabelPath = new javax.swing.JLabel();
        jLabelNome = new javax.swing.JLabel();
        jLabelTitulo = new javax.swing.JLabel();
        jLabelAutor = new javax.swing.JLabel();
        jTextFieldPath = new javax.swing.JTextField();
        jTextFieldNome = new javax.swing.JTextField();
        jTextFieldTitulo = new javax.swing.JTextField();
        jTextFieldAutor = new javax.swing.JTextField();
        jLabelRegras = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jTextFieldConceito = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setResizable(false);

        jSplitPaneMain.setDividerLocation(150);
        jSplitPaneMain.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);

        javax.swing.tree.DefaultMutableTreeNode treeNode1 = new javax.swing.tree.DefaultMutableTreeNode("root");
        jTree1.setModel(new javax.swing.tree.DefaultTreeModel(treeNode1));
        jTree1.setAutoscrolls(true);
        jTree1.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        jTree1.addTreeSelectionListener(new javax.swing.event.TreeSelectionListener() {
            public void valueChanged(javax.swing.event.TreeSelectionEvent evt) {
                jTree1ValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(jTree1);

        javax.swing.GroupLayout jPanelTreeLayout = new javax.swing.GroupLayout(jPanelTree);
        jPanelTree.setLayout(jPanelTreeLayout);
        jPanelTreeLayout.setHorizontalGroup(
            jPanelTreeLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelTreeLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 680, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanelTreeLayout.setVerticalGroup(
            jPanelTreeLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelTreeLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 127, Short.MAX_VALUE)
                .addContainerGap())
        );

        jSplitPaneMain.setLeftComponent(jPanelTree);

        jPanelInformacao.setBorder(javax.swing.BorderFactory.createTitledBorder("Informação"));

        jLabelPath.setText("Caminho");

        jLabelNome.setText("Nome");

        jLabelTitulo.setText("Título");

        jLabelAutor.setText("Autor");

        jTextFieldPath.setEditable(false);

        jTextFieldNome.setEditable(false);

        jTextFieldTitulo.setEditable(false);

        jTextFieldAutor.setEditable(false);

        jLabelRegras.setFont(new java.awt.Font("Dialog", 1, 14));
        jLabelRegras.setForeground(new java.awt.Color(255, 0, 0));
        jLabelRegras.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/abrir.png"))); // NOI18N
        jButton1.setMnemonic('A');
        jButton1.setToolTipText("Abrir o arquivo");
        jButton1.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/conceito.png"))); // NOI18N
        jButton2.setToolTipText("Definir Conceito");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/remover.png"))); // NOI18N
        jButton3.setToolTipText("Remover conceito");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jLabel1.setText("Conceito");

        jTextFieldConceito.setEditable(false);

        javax.swing.GroupLayout jPanelInformacaoLayout = new javax.swing.GroupLayout(jPanelInformacao);
        jPanelInformacao.setLayout(jPanelInformacaoLayout);
        jPanelInformacaoLayout.setHorizontalGroup(
            jPanelInformacaoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelInformacaoLayout.createSequentialGroup()
                .addGroup(jPanelInformacaoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanelInformacaoLayout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanelInformacaoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabelPath)
                            .addComponent(jLabelNome)
                            .addGroup(jPanelInformacaoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(jLabelAutor, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jLabelTitulo, javax.swing.GroupLayout.Alignment.LEADING)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanelInformacaoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jTextFieldAutor, javax.swing.GroupLayout.DEFAULT_SIZE, 603, Short.MAX_VALUE)
                            .addComponent(jTextFieldTitulo, javax.swing.GroupLayout.DEFAULT_SIZE, 603, Short.MAX_VALUE)
                            .addComponent(jTextFieldNome, javax.swing.GroupLayout.DEFAULT_SIZE, 603, Short.MAX_VALUE)
                            .addComponent(jTextFieldPath, javax.swing.GroupLayout.DEFAULT_SIZE, 603, Short.MAX_VALUE)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanelInformacaoLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabelRegras, javax.swing.GroupLayout.DEFAULT_SIZE, 648, Short.MAX_VALUE))
                    .addGroup(jPanelInformacaoLayout.createSequentialGroup()
                        .addGap(120, 120, 120)
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 132, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanelInformacaoLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel1)
                        .addGap(3, 3, 3)
                        .addComponent(jTextFieldConceito, javax.swing.GroupLayout.DEFAULT_SIZE, 603, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanelInformacaoLayout.setVerticalGroup(
            jPanelInformacaoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelInformacaoLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelInformacaoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE, false)
                    .addComponent(jLabelPath)
                    .addComponent(jTextFieldPath, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanelInformacaoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE, false)
                    .addComponent(jLabelNome)
                    .addComponent(jTextFieldNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanelInformacaoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelTitulo)
                    .addComponent(jTextFieldTitulo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanelInformacaoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE, false)
                    .addComponent(jLabelAutor)
                    .addComponent(jTextFieldAutor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanelInformacaoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTextFieldConceito, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 22, Short.MAX_VALUE)
                .addGroup(jPanelInformacaoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jButton3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jButton2))
                .addGap(18, 18, 18)
                .addComponent(jLabelRegras, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(24, 24, 24))
        );

        javax.swing.GroupLayout jPanelTableLayout = new javax.swing.GroupLayout(jPanelTable);
        jPanelTable.setLayout(jPanelTableLayout);
        jPanelTableLayout.setHorizontalGroup(
            jPanelTableLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelTableLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanelInformacao, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanelTableLayout.setVerticalGroup(
            jPanelTableLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelTableLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanelInformacao, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        jSplitPaneMain.setRightComponent(jPanelTable);

        javax.swing.GroupLayout jPanelMainLayout = new javax.swing.GroupLayout(jPanelMain);
        jPanelMain.setLayout(jPanelMainLayout);
        jPanelMainLayout.setHorizontalGroup(
            jPanelMainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelMainLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jSplitPaneMain, javax.swing.GroupLayout.DEFAULT_SIZE, 702, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanelMainLayout.setVerticalGroup(
            jPanelMainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelMainLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jSplitPaneMain, javax.swing.GroupLayout.DEFAULT_SIZE, 544, Short.MAX_VALUE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanelMain, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanelMain, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(22, Short.MAX_VALUE))
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-758)/2, (screenSize.height-637)/2, 758, 637);
    }// </editor-fold>//GEN-END:initComponents

    private void jTree1ValueChanged(javax.swing.event.TreeSelectionEvent evt) {//GEN-FIRST:event_jTree1ValueChanged
        // TODO add your handling code here:
        String node = Configurar.getNode(evt.getPath());

        MapaDoTextoController control = new MapaDoTextoController();
        MapaDoTexto mapa = control.findByNome(node);

        if (mapa != null) {
            jTextFieldPath.setText(mapa.getPath());
            jTextFieldTitulo.setText(mapa.getTitulo());
            jTextFieldNome.setText(mapa.getNome());
            jTextFieldAutor.setText(mapa.getAutor());
            jTextFieldConceito.setText(mapa.getConceito());

            List<Regra> regras = mapa.getRegras();
            if (regras == null || regras.isEmpty() || regras.size() == 0) {
                jLabelRegras.setText("Não existem regras!");
            } else {
                jLabelRegras.setText("As regras já existem para esse arquivo.");
            }
        } else {
            jTextFieldPath.setText("");
            jTextFieldTitulo.setText("");
            jTextFieldNome.setText("");
            jTextFieldAutor.setText("");
            jLabelRegras.setText("");
            jTextFieldConceito.setText("");
        }


    }//GEN-LAST:event_jTree1ValueChanged

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        try {
            Arquivo.abrir(new File(jTextFieldPath.getText()));
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "Falha ao abrir o arquivo\nO sistema diz:\n" + ex.getMessage(), "Erro", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        Configurar conf = new Configurar();

        MapaDoTextoController control = new MapaDoTextoController();
        MapaDoTexto mapat = control.findByNome(jTextFieldNome.getText());

        if (mapat != null) {
            if (mapat.getRegras() != null && mapat.getRegras().size() > 0) {
                try {
                    BuscarPadrao padrao = new BuscarPadrao(mapat.getRegras(), conf.getGrauPalavra(), conf.getGrauFrequecia(), conf.getGrauRelevancia());
                    List conceitos = padrao.comparar();
                    if (conceitos != null && conceitos.size() > 0) {
                        JOptionPane jp = new JOptionPane();
                        jp.setInputValue(null);
                        jp.setMessage("Achei esses conceitos, qual é o certo?");
                        jp.setSelectionValues(conceitos.toArray());
                        jp.setIcon(new ImageIcon(question));
                        JDialog dialogo = jp.createDialog(this, "Conceito");
                        dialogo.setVisible(true);
                        if (jp.getInputValue() != null) {
                            mapat.setConceito((String) jp.getInputValue());
                            control.save(mapat);
                        } else {
                            jp.setInputValue(null);
                            jp.setWantsInput(true);
                            jp.setSelectionValues(null);
                            jp.setMessage("Então, qual o conceito?");
                            jp.setIcon(new ImageIcon(question));
                            dialogo = jp.createDialog(this, "Conceito");
                            dialogo.setVisible(true);
                            if (jp.getInputValue() != null) {
                                mapat.setConceito((String) jp.getInputValue());
                                control.save(mapat);
                            }
                        }
                    } else {
                        Object[] opcao = {"Sim", "Não"};
                        int operacao = JOptionPane.showOptionDialog(this, "Não pude definir um conceito, deseje definir um conceito para o texto?", "Conceito", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, new ImageIcon(question), opcao, opcao[0]);
                        if (operacao == JOptionPane.YES_OPTION) {
                            JOptionPane jp = new JOptionPane();
                            jp.setInputValue(null);
                            jp.setWantsInput(true);
                            jp.setMessage("Qual o conceito?");
                            jp.setIcon(new ImageIcon(question));
                            JDialog dialogo = jp.createDialog(this, "Conceito");
                            dialogo.setVisible(true);
                            if (jp.getInputValue() != null) {
                                mapat.setConceito((String) jp.getInputValue());
                                control.save(mapat);
                            }
                        }
                    }
                } catch (FileNotFoundException ex) {
                    Logger.getLogger(ListaRepositorio.class.getName()).log(Level.SEVERE, null, ex);
                } catch (IOException ex) {
                    Logger.getLogger(ListaRepositorio.class.getName()).log(Level.SEVERE, null, ex);
                }
            } else {
                JOptionPane.showMessageDialog(this, "O texto ainda não possui regras.", "Erro", JOptionPane.ERROR_MESSAGE);
            }
        } else {
            JOptionPane.showMessageDialog(this, "Esse elemento não é um texto, ou não está mapeado.", "Erro", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        MapaDoTextoController control = new MapaDoTextoController();
        MapaDoTexto mapat = control.findByNome(jTextFieldNome.getText());

        if (mapat.getConceito() != null) {
            mapat.setConceito(null);
            control.save(mapat);
            JOptionPane.showMessageDialog(this, "Conceito removido com sucesso!!!", "Conceito", JOptionPane.INFORMATION_MESSAGE);
        }else{
            JOptionPane.showMessageDialog(this, "O texto não possui um conceito.", "Conceito", JOptionPane.WARNING_MESSAGE);
        }
    }//GEN-LAST:event_jButton3ActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabelAutor;
    private javax.swing.JLabel jLabelNome;
    private javax.swing.JLabel jLabelPath;
    private javax.swing.JLabel jLabelRegras;
    private javax.swing.JLabel jLabelTitulo;
    private javax.swing.JPanel jPanelInformacao;
    private javax.swing.JPanel jPanelMain;
    private javax.swing.JPanel jPanelTable;
    private javax.swing.JPanel jPanelTree;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSplitPane jSplitPaneMain;
    private javax.swing.JTextField jTextFieldAutor;
    private javax.swing.JTextField jTextFieldConceito;
    private javax.swing.JTextField jTextFieldNome;
    private javax.swing.JTextField jTextFieldPath;
    private javax.swing.JTextField jTextFieldTitulo;
    private javax.swing.JTree jTree1;
    // End of variables declaration//GEN-END:variables
    private Image question = Toolkit.getDefaultToolkit().getImage(getClass().getResource("/imagens/question.png"));
}
